//
//  GuideViewController.m
//  A段项目
//
//  Created by dllo on 16/5/8.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "GuideViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>

@interface GuideViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong)MPMoviePlayerController *moviePlayer;
@property(nonatomic ,strong)AVAudioSession *avaudioSession;
@property (nonatomic, strong)NSTimer *timer;
@property (nonatomic, strong)UIScrollView *scrollView;
@property (nonatomic, strong)UIPageControl *pageControl;


@end

@implementation GuideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
////    self.view.userInteractionEnabled = YES;
//    [self.view addGestureRecognizer:tap];
    
    /**
     *  设置其他音乐软件播放的音乐不被打断
     */
    
    self.avaudioSession = [AVAudioSession sharedInstance];
    NSError *error = nil;
    [self.avaudioSession setCategory:AVAudioSessionCategoryAmbient error:&error];
    
    NSString *urlStr = [[NSBundle mainBundle]pathForResource:@"1.mp4" ofType:nil];
    
    NSURL *url = [NSURL fileURLWithPath:urlStr];
    
    _moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
    //    _moviePlayer.controlStyle = MPMovieControlStyleDefault;
    [_moviePlayer play];
    [_moviePlayer.view setFrame:self.view.bounds];
    
    [self.view addSubview:_moviePlayer.view];
    _moviePlayer.shouldAutoplay = YES;
    [_moviePlayer setControlStyle:MPMovieControlStyleNone];
    [_moviePlayer setFullscreen:YES];
    
    [_moviePlayer setRepeatMode:MPMovieRepeatModeOne];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playbackStateChanged)
                                                 name:MPMoviePlayerPlaybackStateDidChangeNotification
                                               object:_moviePlayer];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [_moviePlayer.view addGestureRecognizer:tap];
    _moviePlayer.view.userInteractionEnabled = YES;
    UIView *view = [[UIView alloc] initWithFrame:self.view.frame];
    view.backgroundColor = [UIColor clearColor];
    [self.view addSubview:view];
    [view addGestureRecognizer:tap];
    view.tag = 90000;
    [self createView];
    
}

- (void)createView {
    UIView * view = [self.view viewWithTag:90000];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, HEIGHT - 150, WIDTH, 100)];
    self.scrollView.bounces = NO;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.contentSize = CGSizeMake(WIDTH * 6, 0);
    [view addSubview:self.scrollView];
    
    UILabel *label0 = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 4, 0, WIDTH / 2, 100)];
    label0.text = @"分享汗水后你的性感";
    [self.scrollView addSubview:label0];
    
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 4 + WIDTH, 0, WIDTH / 2, 100)];
    label1.text = @"每个动作都精确规范";
    [self.scrollView addSubview:label1];
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 4 + WIDTH * 2, 0, WIDTH / 2, 100)];
    label2.text = @"规划陪伴你的训练过程";
    [self.scrollView addSubview:label2];
    UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH /4 + WIDTH * 3, 0, WIDTH/2, 100)];
    label3.text = @"全程记录你的健身数据";
    [self.scrollView addSubview:label3];
    UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 4 + WIDTH * 4, 0, WIDTH/2, 100)];
    label4.text = @"分享汗水后你的性感";
    [self.scrollView addSubview:label4];
    
    UILabel *label5 = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 4 + WIDTH * 5, 0, WIDTH/2, 100)];
    label5.text = @"每个动作都精确规范";
    [self.scrollView addSubview:label5];
    self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
    self.scrollView.delegate = self;
    label5.textAlignment = 1;
    label5.textColor = [UIColor whiteColor];
    label5.font = [UIFont systemFontOfSize:20];
    label4.textAlignment = 1;
    label4.textColor = [UIColor whiteColor];
    label4.font = [UIFont systemFontOfSize:20];
    label3.textAlignment = 1;
    label3.textColor = [UIColor whiteColor];
    label3.font = [UIFont systemFontOfSize:20];
    label2.textAlignment = 1;
    label2.textColor = [UIColor whiteColor];
    label2.font = [UIFont systemFontOfSize:20];
    label1.textAlignment = 1;
    label1.textColor = [UIColor whiteColor];
    label1.font = [UIFont systemFontOfSize:20];
    label0.textAlignment = 1;
    label0.textColor = [UIColor whiteColor];
    label0.font = [UIFont systemFontOfSize:20];

    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH / 4, HEIGHT - 50, WIDTH / 2, 20)];
    self.pageControl.currentPage = 0;
    [view addSubview:self.pageControl];
    [self.pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];
    self.pageControl.numberOfPages = 4;
    [self setupTimer];
}

- (void)pageChanged:(UIPageControl *)pageControl {
    NSLog(@"jksdf");
//    CGFloat x = ((pageControl.currentPage) + 1) * [UIScreen mainScreen].bounds.size.width;
    
//    [self.scrollView setContentOffset:CGPointMake(x, 0) animated:YES];
//    if (pageControl.currentPage == 0) {
//        
//        self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
//        
//    } else {
//        [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x + WIDTH, 0) animated:YES];
//        
//    }
    
    [self.scrollView setContentOffset:CGPointMake(WIDTH * (pageControl.currentPage + 1), 0) animated:YES];
    

}

-(void)setupTimer{
    
//    self.timer = [NSTimer timerWithTimeInterval:3.0f target:self selector:@selector(timerChanged) userInfo:nil repeats:YES];
    self.timer = [NSTimer timerWithTimeInterval:3.0 target:self selector:@selector(timerChanged) userInfo:nil repeats:YES];
//    [self.timer fire];
    [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSDefaultRunLoopMode];
    
}


-(void)timerChanged{
//    int page  = (self.pageControl.currentPage +1) %4;
    
    NSLog(@"%ld",self.pageControl.currentPage);
//    [self pageChanged:self.pageControl];
//    NSInteger page = self.pageControl.currentPage + 1;
    if (self.scrollView.contentOffset.x == WIDTH * 5) {
        
        self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
    } else {
       [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x + WIDTH, 0) animated:YES];
    
    }
    
    
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x / WIDTH == 5) {
        self.pageControl.currentPage = 0;
    } else {
        self.pageControl.currentPage = scrollView.contentOffset.x / WIDTH - 1;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    double page = self.scrollView.contentOffset.x / self.scrollView.bounds.size.width;
    self.pageControl.currentPage = page;
    
    if (page== - 1)
    {
        self.pageControl.currentPage = 3;// 序号0 最后1页
    }
    else if (page == 4)
    {
        self.pageControl.currentPage = 0; // 最后+1,循环第1页
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
    }
        if (self.scrollView.contentOffset.x == WIDTH * 5) {
    
            self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
            self.pageControl.currentPage = 0;
        } else {
           [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x + WIDTH, 0) animated:YES];
    
        }

}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.timer invalidate];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    [self setupTimer];
    
}


-(void)playbackStateChanged{
    
    
    //取得目前状态
    MPMoviePlaybackState playbackState = [_moviePlayer playbackState];
    
    //状态类型
    switch (playbackState) {
        case MPMoviePlaybackStateStopped:
            [_moviePlayer play];
            break;
            
        case MPMoviePlaybackStatePlaying:
            break;
            
        case MPMoviePlaybackStatePaused:
            [_moviePlayer play];
            break;
            
        case MPMoviePlaybackStateInterrupted:

            break;
            
        case MPMoviePlaybackStateSeekingForward:
            
            break;
            
        case MPMoviePlaybackStateSeekingBackward:
           
            break;
            
        default:
           
            break;
    }
}


- (void)tapAction:(UITapGestureRecognizer *)tap {
    
    self.block();
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.timer invalidate];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
